package com.yancy.controller;

import com.google.gson.Gson;
import com.yancy.entity.New;
import com.yancy.service.NewService;
import com.yancy.service.impl.NewServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/index")
public class GetNewServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        NewService newService = new NewServiceImpl();
        List<New> list = newService.getNew();
//        给前端返回新闻列表 使用gson将list转为前端可以识别的json格式
        Gson gson =  new Gson();
        String json = gson.toJson(list);
        PrintWriter out = resp.getWriter();
        out.print(json);
    }
}
